package org.raymond.iworks;

import lombok.extern.slf4j.Slf4j;
import org.raymond.iworks.study.orders.common.ActivityReadyEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@EnableJpaRepositories(basePackages={"org.raymond.iworks.repository.repository",
        "org.raymond.iworks.study.spring.repository", "org.raymond.iworks.study.orders.repository"})
@EntityScan(basePackages={"org.raymond.iworks.repository.entity",
        "org.raymond.iworks.study.spring.entity", "org.raymond.iworks.study.orders.entity"})
@EnableJpaAuditing
@EnableTransactionManagement
@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy=true)
@ComponentScan(excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,
        pattern="org.raymond.iworks.study.mq.kafka.consumer.+Consumer"))
@EnableScheduling
@Slf4j
public class StudyApplication {
    private final static CountDownLatch LATCH = new CountDownLatch(1);
    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        SpringApplication springApplication = new SpringApplication(StudyApplication.class);
        //springApplication.addListeners(new AppStart21());
        springApplication.addListeners(new ActivityReadyEvent());
        ConfigurableApplicationContext ctx = springApplication.run(args);
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = System.getProperty("server.port");
        String path = System.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application Study is running! Access URLs:\n\t" +
                "Local: \t\thttp://:" + ip + ":" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "----------------------------------------------------------");
        //CompositePattern.exec(args);

        // waitKafkaListener(ctx);
    }

    private static void waitKafkaListener(ConfigurableApplicationContext ctx) throws InterruptedException {
        LATCH.await();
        Thread.sleep(5_000);
        ctx.close();
    }

    // for kafka sample3
    //@KafkaListener(id = "fooGroup3", topics = "topic3")
    public void listen2(List<String> in) {
        log.info("Received: " + in);
        LATCH.countDown();
    }
}
